package com.shenyi.mould.rest.controller.shop;

import com.shenyi.core.utils.PageUtils;
import com.shenyi.core.utils.Query;
import com.shenyi.core.utils.R;
import com.shenyi.core.utils.TreeUtils;
import com.shenyi.shop.api.ICategoryService;
import com.shenyi.shop.entity.CategoryDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/06
 * @desc 分类
 * @see
 * @since 1.0
 */

@RestController
@RequestMapping("category")
public class CategoryController {

        @Autowired
        private ICategoryService categoryService;

        /**
         * 查看列表
        */
        @RequestMapping("/list")
        @RequiresPermissions("category:list")
        public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CategoryDO> categoryList = categoryService.queryList(query);
        int total = categoryService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(categoryList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
        }

        /**
         * 查看信息
        */
        @RequestMapping("/info/{id}")
        @RequiresPermissions("category:info")
        public R info(@PathVariable("id") Integer id) {
        CategoryDO category = categoryService.queryObject(id);

        return R.ok().put("category", category);
        }

        /**
         * 保存
        */
        @RequestMapping("/save")
        @RequiresPermissions("category:save")
        public R save(@RequestBody CategoryDO category) {
        categoryService.save(category);

        return R.ok();
        }

        /**
         * 修改
        */
        @RequestMapping("/update")
        @RequiresPermissions("category:update")
        public R update(@RequestBody CategoryDO category) {
        categoryService.update(category);

        return R.ok();
        }

        /**
         * 删除
        */
        @RequestMapping("/delete")
        @RequiresPermissions("category:delete")
        public R delete(@RequestBody Integer[] ids) {
        categoryService.deleteBatch(ids);

        return R.ok();
        }

/**
 * 查看所有列表
 */
@RequestMapping("/queryAll")
public R queryAll(@RequestParam Map<String, Object> params) {

        List<CategoryDO> list = categoryService.queryList(params);
        //添加顶级菜单
        CategoryDO root = new CategoryDO();
        root.setId(0);
        root.setName("一级分类");
        root.setParentId(-1);
        root.setOpen(true);
        list.add(0,root);
        return R.ok().put("list", list);
        }

/**
 * 查看信息(全部加载页面渲染太慢！)
 */
@RequestMapping("/getAreaTree")
public R getAreaTree() {
        List<CategoryDO> list = categoryService.queryList(new HashMap<String, Object>());
        for (CategoryDO SysRegionDO : list) {
        SysRegionDO.setValue(SysRegionDO.getId() + "");
        SysRegionDO.setLabel(SysRegionDO.getName());
        }
        List<CategoryDO> node = TreeUtils.factorTree(list);

        return R.ok().put("node", node);
        }

/**
 * 查询
 *
 * @return
 */
@RequestMapping("/getCategorySelect")
public R getCategorySelect() {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", "0");
        List<CategoryDO> list = categoryService.queryList(map);
        return R.ok().put("list", list);
        }
}
